//apdxE_cf2_black
/* 1. Appendix E: Counterfactual 2 Estimates of the Effect of Veteran Status on 
	Homeownership and Logged Home Value for Black Men, 1912-1932 Birth Cohorts
	
	2. Generates counterfactual predicted homeownership and home value for if black men had the same 
	education, income, and employment rates as white men 
*/

matrix drop _all
macro drop _all

global dir = "YourFilePath"
global output = "$dir"
global working_data = "$dir/ipums60_pub.dta"

cd "$output"
use "$working_data", clear

codebook educ empstat marst	
set seed 2436918 

* counterfactual logged income 
cap gen cf_ln_inc2018 =. 
cap gen cf_inc_2018 = . 

foreach i in inc_2018 ln_inc2018  {		
	sum `i' if race ==1, d
	local mean_`i'_60 = r(mean)
	local sd_`i'_60 = r(sd)
	di "mean of `i' in 1960 = `mean_`i'_60' & sd = `sd_`i'_60'" 	
	
	replace cf_`i' = rnormal(`mean_`i'_60', `sd_`i'_60') if race == 2
	replace cf_`i' = `i' if race == 1 
			
	replace cf_`i' = round(cf_`i')	
	label var cf_`i' "Counterfactual in which Black men have same mean & sd of `i' as white men" 
	}
	

* Counterfactual Education and employment
	codebook educ empstat
	gen educ_bin = educ >=2 if !missing(educ) 
		label var educ_bin ">= H.S. diploma (1=yes)"
			
	gen empstat_bin = empstat == 1 if !missing(empstat)
	label var empstat_bin "Employment indicator (1=employed)"
			
	foreach i in educ_bin empstat_bin {
		gen cf_`i' = .
		sum `i' if race == 1 
		local mean_`i'_60 = r(mean)
		local sd_`i'_60 = r(sd)
		replace cf_`i' = rnormal(`mean_`i'_60', `sd_`i'_60') if race ==2 
		replace cf_`i' = `i' if race ==1 
		replace cf_`i' = round(cf_`i') 
		}

	global cf_ses cf_ln_inc2018 cf_educ_bin cf_empstat_bin

	global state_fe state_cat2 state_cat3 state_cat4 state_cat5 state_cat6 ///
		state_cat7 state_cat8 state_cat9 state_cat10 state_cat11 state_cat12 state_cat13 ///
		state_cat14 state_cat15 state_cat16 state_cat17 state_cat18 state_cat19 state_cat20 ///
		state_cat21 state_cat22 state_cat23 state_cat24 state_cat25 state_cat26 state_cat27 ///
		state_cat28 state_cat29 state_cat30 state_cat31 state_cat32 state_cat33 state_cat34 /// 
		state_cat35 state_cat36 state_cat37 state_cat38 state_cat39 state_cat40 state_cat41 ///
		state_cat42 state_cat43 state_cat44 state_cat45 state_cat46 state_cat47 state_cat48 ///
		state_cat49 state_cat50
	
	
********************************************************************************
* Appendix E: Counterfactual 2 Estimates of the Effect of Veteran Status on 
*	Homeownership and Logged Home Value for Black Men, 1912-1932 Birth Cohorts
********************************************************************************
* table headers 
	set more off
	putexcel set apdxE_cf2.xls,  replace 

	putexcel B2 = ("Homeownership")  C2 = ("Logged Home Value") 		
	putexcel A3 = ("Stage 1") A5 = ("Reduced Form") A7 = ("IV") A9 = ("OLS") A11 = ("N") 
	
	putexcel A1 = ("Counterfactual 2 Estimates of the Effect of Veteran Status on Homeownership and Logged Home Value for Black Men, 1912-1932 Birth Cohorts")
	putexcel A12 = ("Robust standard errors in parentheses. Models include state fixed effects. Counterfactual 2 gives Black men the same distribution of SES (income, educational attainment, employment status) as white men") 
	putexcel A13 = ("Source: 1960 IPUMS 1% Sample")
	putexcel A14 = ("*** p<0.001, ** p<0.01, * p<0.05, + p<0.10")
	
** HOMEOWNERSHIP OUTCOME *******************************************************
* STAGE 1 **********************************************************************
* Black *
	reg vetbin_60 inst_1927 age $cf_ses $state_fe [weight = perwt] if race == 2, vce(cluster statefip)	
	
		matrix list r(table)
		matrix define reg_output= r(table) 
		local b = string(reg_output[1,1], "%04.3f") 
		local pval =  string(reg_output[4,1], "%04.3f") 
		
		local pval_star = ""
			if (`pval' < 0.001) local pval_star = "***"
			else if (`pval' < 0.01) local pval_star = "**"
			else if (`pval' < 0.05) local pval_star = "*"
			else if (`pval' < 0.10) local pval_star = "+"
		
		local row = 3
		putexcel B`row'= ("`b'`pval_star'")   
		
		local row = `row' + 1  
			
		
		local se = string(reg_output[2,1], "%04.3f")
		putexcel B`row'= ("(`se')") 

* REDUCED FORM *****************************************************************
* Black *
	reg ownbin inst_1927 age $cf_ses $state_fe [weight = perwt] if race == 2, vce(cluster statefip)
	
		matrix list r(table)
		matrix define reg_output= r(table) 
		local b = string(reg_output[1,1], "%04.3f") 
		local pval =  string(reg_output[4,1], "%04.3f") 
		
	local pval_star = ""
		if (`pval' < 0.001) local pval_star = "***"
		else if (`pval' < 0.01) local pval_star = "**"
		else if (`pval' < 0.05) local pval_star = "*"
		else if (`pval' < 0.10) local pval_star = "+"
	
	local row = 5
		
	putexcel B`row'= ("`b'`pval_star'")   
		
	local row = `row' + 1  	
		
	local se = string(reg_output[2,1], "%04.3f")
	putexcel B`row'= ("(`se')") 
	

* STAGE 2 (IV) *****************************************************************
* Black *
ivreg ownbin age $cf_ses $state_fe (vetbin_60=inst_1927) [weight = perwt] if race == 2, cluster (statefip)

	predict s2_black_60 if e(sample)
	bysort vetbin_60: sum s2_black_60 
	
	local pval = 2 * ttail(e(df_r), abs(_b[vetbin] / _se[vetbin]))
	local pval_star = ""
		if (`pval' <= 0.001) local pval_star = "***"
		else if (`pval' <= 0.01) local pval_star = "**"
		else if (`pval' <= 0.05) local pval_star = "*"
		else if (`pval' <= 0.10) local pval_star = "+"
		
		local row = 7 
		
		local b = string(_b[vetbin_`i'] ,"%12.3f")
		putexcel B`row'= ("`b'`pval_star'")   
			
		local row = `row'+1  
			
		local se = string(_se[vetbin_`i'], "%12.3f")
		putexcel B`row'= ("(`se')") 

* OLS **************************************************************************
* Black *
	reg ownbin vetbin_60 age $cf_ses $state_fe [weight = perwt] if race == 2, vce(cluster statefip)
	
		matrix list r(table)
		matrix define reg_output= r(table) 
		local b = string(reg_output[1,1], "%04.3f") 
		local pval =  string(reg_output[4,1], "%04.3f") 
		local obs = string(e(N), "%12.0gc")
		di "b = `b'; pval = `pval'; N=`obs'"
		
	local pval_star = ""
		if (`pval' < 0.001) local pval_star = "***"
		else if (`pval' < 0.01) local pval_star = "**"
		else if (`pval' < 0.05) local pval_star = "*"
		else if (`pval' < 0.10) local pval_star = "+"
	
	local row = 9
		
	putexcel B`row'= ("`b'`pval_star'")   
		
	local row = `row' + 1  	
		
	local se = string(reg_output[2,1], "%04.3f")
	di `se'
	putexcel B`row'= ("(`se')") 
	local row = `row' + 1  	
	putexcel B`row'= ("`obs'") 

		
** HOME VALUE OUTCOME **********************************************************	
* STAGE 1  *********************************************************************
	* Black *
	reg vetbin_60 inst_1927 age $cf_ses $state_fe [weight = perwt] if race ==2, vce(cluster statefip)	
	
		matrix list r(table)
		matrix define reg_output= r(table) 
		local b = string(reg_output[1,1], "%04.3f") 
		di `b'
		local pval =  string(reg_output[4,1], "%04.3f") 
		di `pval'
		
		* Assign significance stars
		local pval_star = ""
			if (`pval' < 0.001) local pval_star = "***"
			else if (`pval' < 0.01) local pval_star = "**"
			else if (`pval' < 0.05) local pval_star = "*"
			else if (`pval' < 0.10) local pval_star = "+"
		
		local row = 3
		putexcel C`row'= ("`b'`pval_star'")   
		
		local row = `row' + 1  
			
		
		local se = string(reg_output[2,1], "%04.3f")
		di `se'
		putexcel C`row'= ("(`se')") 
		
	
* REDUCED FORM 1  *********************************************************************
* BLACK * 	
	reg ln_valueh_2018 inst_1927 age $cf_ses $state_fe [weight = perwt] if race == 2, vce(cluster statefip)
	
		matrix list r(table)
		matrix define reg_output= r(table) 
		local b = string(reg_output[1,1], "%04.3f") 
		local pval =  string(reg_output[4,1], "%04.3f") 
		
	* Assign significance stars
	local pval_star = ""
		if (`pval' < 0.001) local pval_star = "***"
		else if (`pval' < 0.01) local pval_star = "**"
		else if (`pval' < 0.05) local pval_star = "*"
		else if (`pval' < 0.10) local pval_star = "+"
	
	local row = 5
		
	putexcel C`row'= ("`b'`pval_star'")   
		
	local row = `row' + 1  	
		
	local se = string(reg_output[2,1], "%04.3f")
	putexcel C`row'= ("(`se')") 


* S2 (Fuzzy Regression Discontinuity)  *****************************************
* BLACK *
	ivreg ln_valueh_2018 age $cf_ses $state_fe (vetbin_60=inst_1927) [weight = perwt] if race == 2, cluster (statefip)
	
	predict iv60_val_black if e(sample)
	replace iv60_val_black = exp(iv60_val_black)
	bysort vetbin_60: sum iv60_val_black 

	local pval = 2 * ttail(e(df_r), abs(_b[vetbin] / _se[vetbin]))
	local pval_star = ""
		if (`pval' <= 0.001) local pval_star = "***"
		else if (`pval' <= 0.01) local pval_star = "**"
		else if (`pval' <= 0.05) local pval_star = "*"
		else if (`pval' <= 0.10) local pval_star = "+"
		
		* Place values in excel	
		local row = 7 
		
		local b = string(_b[vetbin_`i'] ,"%12.3f")
		putexcel C`row'= ("`b'`pval_star'")   
			
		local row = `row'+1  
			
		local se = string(_se[vetbin_`i'], "%12.3f")
		putexcel C`row'= ("(`se')") 

* OLS ************************************************************************** 
* BLACK * 	
	reg ln_valueh_2018 vetbin_60 age $cf_ses $state_fe [weight = perwt] if race == 2, vce(cluster statefip)
	
		matrix list r(table)
		matrix define reg_output= r(table) 
		local b = string(reg_output[1,1], "%04.3f") 
		local pval =  string(reg_output[4,1], "%04.3f") 
		local obs = string(e(N), "%12.0gc")
		di "b = `b'; pval = `pval'; N=`obs'"
		
	local pval_star = ""
		if (`pval' < 0.001) local pval_star = "***"
		else if (`pval' < 0.01) local pval_star = "**"
		else if (`pval' < 0.05) local pval_star = "*"
		else if (`pval' < 0.10) local pval_star = "+"
	
	local row = 9
		
	putexcel C`row'= ("`b'`pval_star'")   
		
	local row = `row' + 1  	
		
	local se = string(reg_output[2,1], "%04.3f")

	putexcel C`row'= ("(`se')") 
	local row = `row' + 1  	
	putexcel C`row'= ("`obs'") 
